import BaseThought
import operator
import gtk


class ThoughtEnvelop():
    def __init__(self,save,selected,color):
        self.tmpulx = 0
        self.tmpuly = 0
        self.tmplrx = 0
        self.tmplry = 0

        if selected:
            self.thoughts = selected
            self.setBoundings()
            self.red = color.red
            self.green = color.green
            self.blue = color.blue
        else:
            self.thoughts = []

        self.element = save.createElement ("envelop")
        self.isenvelop = True 
                
    def setBoundings(self):
        if self.thoughts:
            self.thoughts.sort(key=lambda obj:obj.ul[0])
            self.ulx = self.thoughts[0]
            self.thoughts.sort(key=lambda obj:obj.ul[1])
            self.uly = self.thoughts[0]
            self.thoughts.sort(key=lambda obj:obj.lr[0])
            self.lrx = self.thoughts[len(self.thoughts)-1]
            self.thoughts.sort(key=lambda obj:obj.lr[1])
            self.lry = self.thoughts[len(self.thoughts)-1]
            return True
        return False

    def draw(self,context,negative):
        #draw bounding box
        context.move_to (self.ulx.ul[0]-10, self.uly.ul[1]-5)
        context.line_to (self.ulx.ul[0]-10, self.lry.lr[1]+5)
        context.curve_to (self.ulx.ul[0]-10, self.lry.lr[1]+10, self.ulx.ul[0]-10, self.lry.lr[1]+10, self.ulx.ul[0]-5, self.lry.lr[1]+10)
        context.line_to (self.lrx.lr[0]+5, self.lry.lr[1]+10)
        context.curve_to (self.lrx.lr[0]+10, self.lry.lr[1]+10, self.lrx.lr[0]+10, self.lry.lr[1]+10, self.lrx.lr[0]+10, self.lry.lr[1]+5)
        context.line_to (self.lrx.lr[0]+10, self.uly.ul[1]-5)
        context.curve_to (self.lrx.lr[0]+10, self.uly.ul[1]-10, self.lrx.lr[0]+10, self.uly.ul[1]-10, self.lrx.lr[0]+5, self.uly.ul[1]-10)
        context.line_to (self.ulx.ul[0]-5, self.uly.ul[1]-10)
        context.curve_to (self.ulx.ul[0]-10, self.uly.ul[1]-10, self.ulx.ul[0]-10, self.uly.ul[1]-10, self.ulx.ul[0]-10, self.uly.ul[1]-5)
        #draw closing cross
        context.move_to(self.ulx.ul[0]-15,self.uly.ul[1]-15)
        context.line_to(self.ulx.ul[0]-5,self.uly.ul[1]-15)
        context.line_to(self.ulx.ul[0]-5,self.uly.ul[1]-5)
        context.line_to(self.ulx.ul[0]-15,self.uly.ul[1]-5)
        context.line_to(self.ulx.ul[0]-15,self.uly.ul[1]-15)
        context.move_to(self.ulx.ul[0]-10,self.uly.ul[1]-15)
        context.line_to(self.ulx.ul[0]-10,self.uly.ul[1]-5)
        context.move_to(self.ulx.ul[0]-5,self.uly.ul[1]-10)
        context.line_to(self.ulx.ul[0]-15,self.uly.ul[1]-10)

        r,g,b = self.gtk_to_cairo_color()
        if negative:
            r = 1.0-r
            g = 1.0-g
            b = 1.0-b
        context.set_source_rgb(r,g,b)
        context.fill_preserve ()
        if not negative:
            context.set_source_rgb (0,0,0)
        else:
            context.set_source_rgb(1.0,1.0,1.0)
        context.stroke ()

    def export(self,context,move_x,move_y,negative):
        context.move_to (move_x+self.ulx.ul[0]-10, move_y+self.uly.ul[1]-5)
        context.line_to (move_x+self.ulx.ul[0]-10, move_y+self.lry.lr[1]+5)
        context.curve_to (move_x+self.ulx.ul[0]-10, move_y+self.lry.lr[1]+10, move_x+self.ulx.ul[0]-10, move_y+self.lry.lr[1]+10, move_x+self.ulx.ul[0]-5, move_y+self.lry.lr[1]+10)
        context.line_to (move_x+self.lrx.lr[0]+5, move_y+self.lry.lr[1]+10)
        context.curve_to (move_x+self.lrx.lr[0]+10, move_y+self.lry.lr[1]+10, move_x+self.lrx.lr[0]+10, move_y+self.lry.lr[1]+10, move_x+self.lrx.lr[0]+10, move_y+self.lry.lr[1]+5)
        context.line_to (move_x+self.lrx.lr[0]+10, move_y+self.uly.ul[1]-5)
        context.curve_to (move_x+self.lrx.lr[0]+10, move_y+self.uly.ul[1]-10, move_x+self.lrx.lr[0]+10, move_y+self.uly.ul[1]-10, move_x+self.lrx.lr[0]+5, move_y+self.uly.ul[1]-10)
        context.line_to (move_x+self.ulx.ul[0]-5, move_y+self.uly.ul[1]-10)
        context.curve_to (move_x+self.ulx.ul[0]-10, move_y+self.uly.ul[1]-10, move_x+self.ulx.ul[0]-10, move_y+self.uly.ul[1]-10, move_x+self.ulx.ul[0]-10, move_y+self.uly.ul[1]-5)
        #draw closing cross
        context.move_to(move_x+self.ulx.ul[0]-15,move_y+self.uly.ul[1]-15)
        context.line_to(move_x+self.ulx.ul[0]-5,move_y+self.uly.ul[1]-15)
        context.line_to(move_x+self.ulx.ul[0]-5,move_y+self.uly.ul[1]-5)
        context.line_to(move_x+self.ulx.ul[0]-15,move_y+self.uly.ul[1]-5)
        context.line_to(move_x+self.ulx.ul[0]-15,move_y+self.uly.ul[1]-15)
        context.move_to(move_x+self.ulx.ul[0]-10,move_y+self.uly.ul[1]-15)
        context.line_to(move_x+self.ulx.ul[0]-10,move_y+self.uly.ul[1]-5)
        context.move_to(move_x+self.ulx.ul[0]-5,move_y+self.uly.ul[1]-10)
        context.line_to(move_x+self.ulx.ul[0]-15,move_y+self.uly.ul[1]-10)

        r,g,b = self.gtk_to_cairo_color()
        if negative:
            r = 1.0-r
            g = 1.0-g
            b = 1.0-b
        context.set_source_rgb(r,g,b)
        context.fill_preserve ()
        if not negative:
            context.set_source_rgb (0,0,0)
        else:
            context.set_source_rgb(1.0,1.0,1.0)
        context.stroke ()

    def gtk_to_cairo_color(self):
        return (self.red / 65535.0, self.green / 65535.0, self.blue / 65535.0)


    def includes (self, coords, mode):
        if coords[0]<self.ulx.ul[0]-15:
            return False
        if coords[1]<self.uly.ul[1]-15:
            return False
        if coords[0]>self.ulx.ul[0]-5:
            return False
        if coords[1]>self.uly.ul[1]-5:
            return False

        return True

    def handle_motion (self, event, mode, transformed):
        pass

    def want_motion (self):
        pass

    def process_button_down (self, event, mode, transformed):
        pass


    def update_save (self):
        self.element.setAttribute ("ulx", str(self.ulx.ul[0]))
        self.element.setAttribute ("uly", str(self.uly.ul[1]))
        self.element.setAttribute ("lrx", str(self.lrx.lr[0]))
        self.element.setAttribute ("lry", str(self.lry.lr[1]))
        self.element.setAttribute ("red", str(self.red))
        self.element.setAttribute ("green",str(self.green))
        self.element.setAttribute ("blue",str(self.blue))

    def process_button_release(self, event, unending_link, mode, transformed):
        pass

    def get_save_element(self):
        return self.element

    def load (self, node):
        #oops, seems like something happened there ?
        tmp = node.getAttribute("ulx")
        if not tmp:
            return False

        self.tmpulx = float(node.getAttribute("ulx"))
        self.tmpuly = float(node.getAttribute("uly"))
        self.tmplrx = float(node.getAttribute("lrx"))
        self.tmplry = float(node.getAttribute("lry"))
        self.red = int(node.getAttribute("red"))
        self.green = int(node.getAttribute("green"))
        self.blue = int(node.getAttribute("blue"))
        return True
    
    def contains(self,thought):
        if thought.ul[0] < self.tmpulx - 5:
            return
        if thought.ul[1] < self.tmpuly - 5:
            return
        if thought.lr[0] > self.tmplrx + 5:
            return
        if thought.lr[1] > self.tmplry + 5:
            return

        self.thoughts.append(thought)
